//
//  VIPComicsCell.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/11.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class VIPComicsCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.addSubview(self.iconView)
        self.iconView.addSubview(self.typeLabel)
        self.contentView.addSubview(self.titleLabel)
        setUpSnap()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var iconView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.clipsToBounds = true
        return imgV
    }()
    
    fileprivate lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    fileprivate lazy var typeLabel: UILabel = {
       let label = UILabel()
        label.backgroundColor = kRGBA(r: 255, g: 69, b: 0, a: 1)
        label.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        label.text = "免费试读"
        label.textAlignment = .center
        label.addBorderLeft(size: 10, color: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    
    fileprivate func setUpSnap() {
        self.titleLabel.snp.makeConstraints { (make) in
            make.right.left.bottom.equalToSuperview().inset(UIEdgeInsetsMake(0, 10, 0, 10))
            make.height.equalTo(25)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        self.iconView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(self.titleLabel.snp.top)
        }
        
        self.typeLabel.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.height.equalTo(20)
            make.width.equalTo(self.iconView.snp.width).multipliedBy(0.5)
        }
        ////label左上圆角
        let time: TimeInterval = 0.01
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
            let maskPath = UIBezierPath(roundedRect: CGRect(x:0 , y:0 , width:self.typeLabel.width , height:self.typeLabel.height ), byRoundingCorners: .topLeft, cornerRadii: CGSize(width: 10, height: 10))
            let maskLayer = CAShapeLayer()
            maskLayer.frame = CGRect(x:0 , y:0 , width:self.typeLabel.width , height:self.typeLabel.height )
            maskLayer.path = maskPath.cgPath
            self.typeLabel.layer.mask = maskLayer
            self.typeLabel.layer.masksToBounds = true
        }
        
    }
    
    var model: VIPComicsModel? {
        didSet{
            guard model != nil else {
                return
            }
            iconView.kf.setImage(urlString: model?.cover)
            titleLabel.text = model?.name ?? ""
        }
    }
    
}
